
cc.Class({
    extends: cc.Component,

    properties: {
        chengzhi:cc.SpriteFrame,
        cola:cc.SpriteFrame,
        Type:0,
        Po:cc.p(0,0)
    },
    // LIFE-CYCLE CALLBACKS:
    // onLoad () {},
    start () {
        this.node.setPosition(this.Po);
        this.nodeClone = cc.instantiate(this.node);
        this.game = cc.find("Canvas").getComponent('Home');

        this.typeArr = ['coffee','chengzhi','kele'];
        this.allTime = cc.configData.material[this.typeArr[this.Type]].time[cc.configData.material[this.typeArr[this.Type]].level];//减去汉堡本身的价值；
        this.duringTime = 0;
        this.startJishi = false;

        this.node.parent.on('click',this['clickBtn'+this.Type],this);
        this.place = false;
        this.placeNode = null;//放置的汉堡节点
        this.MakeContent = ''+this.Type;// coffee  chengzhi  kele
        this.speedTool();
    },
    clickBtn0(){
        if(this.startJishi ) return;
        cc.voicePlay.playEffect('click_game');
        this.audioPlay = cc.audioEngine.play(cc.voicePlay.url['waterdoing'], true,cc.voicePlay.volume);
        this.startJishi =true;
        this.node.parent.children[1].active = true;
        this.node.children[0].active = true;
    },
    clickBtn1(){
        if(this.startJishi ) return;
        cc.voicePlay.playEffect('click_game');
        this.audioPlay = cc.audioEngine.play(cc.voicePlay.url['waterdoing'], true,cc.voicePlay.volume);
        this.startJishi =true;
        this.node.parent.children[0].active = true;
    },
    clickBtn2(){
        if(this.startJishi ) return;
        cc.voicePlay.playEffect('click_game');
        this.audioPlay = cc.audioEngine.play(cc.voicePlay.url['waterdoing'], true,cc.voicePlay.volume);
        this.startJishi =true;
        this.node.parent.children[0].active = true;
    },
    Dsuceess0(){
        cc.audioEngine.stop(this.audioPlay);
        this.node.parent.children[1].active = false;
        this.node.parent.off('click',this.clickBtn0,this);
        this.drag();
    },
    Dsuceess1(){
        cc.audioEngine.stop(this.audioPlay);
        this.node.parent.children[0].active = false;
        this.node.parent.off('click',this.clickBtn1,this);
        this.node.getComponent(cc.Sprite).spriteFrame = this.chengzhi;
        this.node.children[0].active = true;
        this.node.parent.children[1].height = 0;
        this.drag();
    },
    Dsuceess2(){
        cc.audioEngine.stop(this.audioPlay);
        this.node.parent.children[0].active = false;
        this.node.getComponent(cc.Sprite).spriteFrame = this.cola;
        this.node.parent.off('click',this.clickBtn2,this);
        this.node.children[0].active = true;
        this.node.parent.children[1].height = 0;
        this.drag();
    },
    drag(){
        cc.voicePlay.playEffect('foodok');
        this.parentInit = null;
        this.node.on('touchstart',function(event){
            if(!this.game.singleDrag) return;
            this.game.singleDrag = false;
            if(this.parentInit) return;
            this.parentInit = this.node.parent;
            this.node.parent = cc.find('Canvas');
            var po = cc.find('Canvas').convertToNodeSpaceAR(this.parentInit.convertToWorldSpaceAR(cc.p(this.node.x,this.node.y)));
            this.node.setPosition(po);
            this.node.zIndex=99;
        },this);
        this.node.on('touchmove',function(event){
            if(!this.parentInit) return;
            var touches = event.getTouches();
            this.node.x += touches[0].getDelta().x;
            this.node.y += touches[0].getDelta().y;
        },this);
        this.node.on('touchend',function(event){
            if(!this.parentInit) return;
            if(this.place && (this.placeNode.group == 'person')){
                if(this.placeNode.getComponent('guest').accounts('cup',this.MakeContent)){
                    this.game.singleDrag = true;
                    this.deleteCup();
                    return;
                }
            }
            this.node.parent = this.parentInit;
            this.parentInit = null;
            this.node.setPosition(this.Po);
            this.game.singleDrag = true;
        },this);
        this.node.on('touchcancel',function(event){
            if(!this.parentInit) return;
            if(this.place && (this.placeNode.group == 'person')){
                if(this.placeNode.getComponent('guest').accounts('cup',this.MakeContent)){
                    this.game.singleDrag = true;
                    this.deleteCup();
                    return;
                }
            }
            this.node.parent = this.parentInit;
            this.parentInit = null;
            this.node.setPosition(this.Po);
            this.game.singleDrag = true;
        },this)
    },
    onCollisionEnter: function (other, self) {
        console.log('enter',other.node.group);
        if(other.node.group != 'person') return;
        this.place = true;
        this.placeNode = other.node;
    },
    onCollisionExit: function (other, self) {
        console.log('exit',other.node.group);
        if(other.node.group != 'person') return;
        this.place = false;
    },
    onDestroy(){
        this.parentInit && this.parentInit.addChild(this.nodeClone);
    },
    deleteCup(){
        var price = cc.configData.material[this.typeArr[this.Type]].price[cc.configData.material[this.typeArr[this.Type]].level];//减去汉堡本身的价值；
        cc.coinWin += price;
        if(this.Type == 0 ){
            this.parentInit.children[0].children[0].getComponent(cc.Sprite).fillRange = 0;
        }
        this.node.destroy();
    },
    speedTool(){
        cc.find('Canvas').on('speedDrink',function(){
            if(this.startJishi && this.duringTime < this.allTime - 2){
                this.duringTime = this.allTime - 2;
            }
        }.bind(this));
    },
    update (dt) {
        if(!this.startJishi) return;
        if(this.duringTime < this.allTime){
            this.duringTime +=  cc.speedDrink*dt;
            if(this.Type == 1 || this.Type == 2){
                this.node.parent.children[1].height = 88 * this.duringTime / this.allTime;
            }
            if(this.Type == 0 ){
                this.node.parent.children[0].children[0].getComponent(cc.Sprite).fillRange = this.duringTime / this.allTime;
            }
        }else{
            this.startJishi = false;
            this['Dsuceess' + this.Type]();
        }
    },
});
